<div class="row">
	<div class="col-lg-4 col-xs-12 nopadding-right">
		<div class="widget flat radius-bordered">
			<div class="widget-header bg-sky theme">
				<span class="widget-caption">工程默认视图</span>
			</div>
			<div class="widget-body" style="padding: 0px; min-height: 905px; max-height: 873px;  overflow: auto;">
				<div id="projectDirTree" class="tree">
					<div class="tree-folder" style="display: none;">
						<div class="tree-folder-header">
							<i class="fa fa-folder yellow"></i>
							<div class="tree-folder-name"></div>
						</div>
						<div class="tree-folder-content"></div>
						<div class="tree-loader" style="display: none;"></div>
					</div>
					<div class="tree-item" style="display: none;">
						<i class="tree-dot"></i>
						<div class="tree-item-name"></div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div class="col-lg-8 col-xs-12">
		<div class="widget flat radius-bordered">
			<div class="widget-header bg-sky theme">
				<span class="widget-caption">参数设置</span>
			</div>
			<div class="widget-body">
				<p class="form-title">基础参数设置</p>
				<div id="registration-form">
					<form class="form-horizontal" action="codeGenerateController/showSecondStepSettingPageView.do" method="post" responseType="html" role="form" id="codeGenerateParamsFrom" isCanRefresh="true"
                              data-bv-message="This value is not valid"
                              data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
                              data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
                              data-bv-feedbackicons-validating="glyphicon glyphicon-refresh">
                        <input type="hidden" name="formToken" value="${TempToken}">
						<div class="form-group">
							<label for="author"
								class="col-sm-2 control-label no-padding-right">开发者姓名</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="author" id="author"
									placeholder="开发者姓名" title="开发者姓名" value="${defaultConfig.author}" readonly="readonly"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty">
							</div>
						</div>
						<div class="form-group">
							<label for="projectDir"
								class="col-sm-2 control-label no-padding-right">工程绝对路径</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="projectDir" id="projectDir"
									placeholder="单击选择工程目录" title="单击选择工程目录" value="${defaultConfig.projectDir}" readonly="readonly"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty">
								<p class="red">注意：此处为工程开发过程中对应的实际工作空间,而不是发布路径</p>
							</div>
						</div>
						<div class="form-group">
							<label for="templatesMapping"
								class="col-sm-2 control-label no-padding-right">模板映射</label>
							<div class="col-sm-10">
								<input type=text class="form-control" name="templatesMapping" id="templatesMapping"
									placeholder="templatesMapping"
									value="${defaultConfig.templatesMapping}"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty"
                                    data-bv-regexp="true"
                                    data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                    data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
								<p class="red">配置规则：模板名称->生成的文件保存的包名。可同时配置多个,每个模板用逗号隔开,如：当模块名为platform时,Idao.java->dao则对应的将保存到com.sumei.platform.dao</p>
							</div>
						</div>
						<div class="form-group">
							<label for="module"
								class="col-sm-2 control-label no-padding-right">模块名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="module" id="module"
									placeholder="module" value="${defaultConfig.module}"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty"
                                    data-bv-regexp="true"
                                    data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                    data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
								<p class="red">注意：最终映射形式为com/wmp/模块</p>
							</div>
						</div>
						<div class="form-group">
							<label for="templateDir"
								class="col-sm-2 control-label no-padding-right">模板位置</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="templateDir" id="templateDir"
									placeholder="templateDir"
									value="${defaultConfig.relativeTemplateDir}"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty"
                                    data-bv-regexp="true"
                                    data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                    data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
								<p class="red">注意：此路径是相对于工程绝对路径</p>
							</div>
						</div>
						<div class="form-group">
							<label for="javaFileOutDir"
								class="col-sm-2 control-label no-padding-right">java文件输出目录</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="javaFileOutDir" id="javaFileOutDir"
									placeholder="javaFileOutDir"
									value="${defaultConfig.relativeJavaFileOutDir}"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty"
                                    data-bv-regexp="true"
                                    data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                    data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
								<p class="red">注意：此路径是相对于工程绝对路径</p>
							</div>
						</div>
						<div class="form-group">
							<label for="viewFileOutDir"
								class="col-sm-2 control-label no-padding-right">视图文件目录</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="viewFileOutDir" id="viewFileOutDir"
									placeholder="viewFileOutDir"
									value="${defaultConfig.relativeViewFileOutDir}"
								  	data-bv-message="The field is not valid"
                                    data-bv-notempty="true"
                                    data-bv-notempty-message="The field is required and cannot be empty"
                                    data-bv-regexp="true"
                                    data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                    data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
								<p class="red">注意：此路径是相对于工程绝对路径</p>
							</div>
						</div>
						<p class="form-title" style="clear: left;">数据源设置</p>
						<div class="form-group">
							<label for="javaFileOutDir" class="col-sm-2 control-label no-padding-right">连接驱动</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="jdbcDriver" id="jdbcDriver" 
								placeholder="数据库连接驱动"
								value="${defaultConfig.jdbcDriver}"
							  	data-bv-message="The field is not valid"
                                data-bv-notempty="true"
                                data-bv-notempty-message="The field is required and cannot be empty"
                                data-bv-regexp="true"
                                data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
							</div>
						</div>			
						<div class="form-group">
							<label for="javaFileOutDir" class="col-sm-2 control-label no-padding-right">数据库服务地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="jdbcUrl" id="jdbcUrl" 
								placeholder="数据库连接URL"
								value="${defaultConfig.jdbcUrl}"
							  	data-bv-message="The field is not valid"
                                data-bv-notempty="true"
                                data-bv-notempty-message="The field is required and cannot be empty"
                                data-bv-regexp="true"
                                data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
                                <p class="red">注意：此地址为数据库服务器地址,请不要指向特定的某一个库</p>
							</div>
						</div>
						<div class="form-group">
							<label for="javaFileOutDir" class="col-sm-2 control-label no-padding-right">用户名</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="jdbcUserName" id="jdbcUserName" 
								placeholder="用户名"
								value="${defaultConfig.jdbcUserName}"
							  	data-bv-message="The field is not valid"
                                data-bv-notempty="true"
                                data-bv-notempty-message="The field is required and cannot be empty"
                                data-bv-regexp="true"
                                data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
							</div>
						</div>
						<div class="form-group">
							<label for="javaFileOutDir" class="col-sm-2 control-label no-padding-right">密码</label>
							<div class="col-sm-10">
								<input type="password" class="form-control" name="jdbcPassword" id="jdbcPassword" 
								placeholder="密码"
								value="${defaultConfig.jdbcPassword}"
							  	data-bv-message="The field is not valid"
                                data-bv-notempty="true"
                                data-bv-notempty-message="The field is required and cannot be empty"
                                data-bv-regexp="true"
                                data-bv-regexp-regexp="[a-zA-Z0-9_\.]+"
                                data-bv-regexp-message="The field can only consist of alphabetical, number, dot and underscore">
							</div>
						</div>	
						<div class="form-group">
							<div class="col-sm-offset-5 col-sm-7">
								<button type="submit" class="btn btn-default">保存任务</button>
								<button type="button" class="btn btn-palegreen" onclick="testConnection()">测试连接</button>
								<button type="submit" class="btn btn-blue">下  一  步</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="row" id="discRow" style="display:none;">
    <div class="col-lg-12">
		<form class="form-horizontal" role="form">
           	<div class="form-group">
				<label for="inputPassword3" class="col-sm-2 control-label no-padding-right">当前选中：</label>
				<div class="col-sm-10">
					<input type="text" class="form-control nowChooseDir" placeholder="点击下面的文件夹来选择" >
				</div>
			</div>
		</form>
        <div class="widget flat radius-bordered">
            <div class="widget-header bg-sky theme">
                <span class="widget-caption">磁盘管理</span>
                <div class="widget-buttons">
                    <a href="#" data-toggle="collapse">
                        <i class="fa fa-minus"></i>
                    </a>
                </div>
            </div>
            <div class="widget-body" style="padding: 0px;">
                <div id="discTree" class="tree tree-unselectable">
                    <div class="tree-folder" style="display: none;">
                        <div class="tree-folder-header">
                            <i class="fa fa-folder yellow"></i>
                            <div class="tree-folder-name"></div>
                        </div>
                        <div class="tree-folder-content">
                        </div>
                        <div class="tree-loader" style="display: none;"></div>
                    </div>
                    <div class="tree-item" style="display: none;">
                        <i class="tree-dot"></i>
                        <div class="tree-item-name"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/js/fuelux/treeview/tree-custom.min.js"></script>
<script src="assets/js/validation/bootstrapValidator_init.js"></script>
<script>
$(function() {
	$("#codeGenerateParamsFrom").on("ajaxSubmitCallback",function(event,data){
		testConnection(function(){
			setMainViewHtml(data, false);
		});
	});
	
	$("#tree-selected-items").on("click",function() {
		console.log("selected items: ", $("#projectDirTree").tree('selectedItems'));
	});

	$("#projectDirTree").on("loaded", function(evt, data) {
		console.log("tree content loaded："+data);
	});

	$("#projectDirTree").on("opened", function(evt, data) {
		console.log("sub-folder opened: ", data);
	});

	$("#projectDirTree").on("closed", function(evt, data) {
		console.log("sub-folder closed: ", data);
	});

	$("#projectDirTree").on("selected", function(evt, data) {
		console.log("item selected: ", data);
	});
	
	$("#discTree").on("opened", function(evt, data) {
		console.log("sub-folder opened: ", data.params.path);
		var nowDir="";
		if(data.params&&data.params.path){
			nowDir=data.params.path;
		}
		$(".nowChooseDir").val(nowDir);
	});

	
	$("#projectDir").on("click",function(){
		bootbox.dialog({
			message : $("#discRow").clone(true).show(),
			title : "磁盘管理",
			className : "modal-darkorange",
			buttons : {
				success : {
					label : "确认",
					className : "btn-blue",
					callback : function() {
						$("#projectDir").val($(".nowChooseDir").val());
					}
				},
				"取消" : {
					className : "btn-danger",
					callback : function() {
						
					}
				}
			}
		});
	});
});
var DataSourceTree = function (options) {
    this._data = options.data;
    this._delay = options.delay;
};
DataSourceTree.prototype = {
    data: function (options, callback) {
    	if(options.params&&options.params.uuid){
    		console.log(options.params.uuid);
    	}
        var self = this;
        setTimeout(function () {
            var data = $.extend(true, [], self._data);
        	var childrenData=options.data;
        	if(childrenData){
        		if(childrenData.length>0){
        			data=childrenData;
        		}else{
        			data=[];
        		}
        	}
            callback({ data: data });
        }, this._delay);
    }
};

var AjaxDataSourceTree = function (options) {
    this._data = options.data;
    this._delay = options.delay;
    this._loadData=options.loadData;
    this._isInit=false;
};
AjaxDataSourceTree.prototype = {
    data: function (options, callback) {
    	if(options.params&&options.params.uuid){
    		console.log(options.params.uuid);
    	}
        var self = this;
        if(self._isInit && self._loadData && (typeof self._loadData === "function")){
        	self._loadData(options,function(data){
        		if(!data){
        			data=[];
        		}
	            callback({ data: data });
        	});
        }else{
	        setTimeout(function () {
	            var data = $.extend(true, [], self._data);
	        	var childrenData=options.data;
	        	if(childrenData){
	        		if(childrenData.length>0){
	        			data=childrenData;
	        		}else{
	        			data=[];
	        		}
	        	}
	            callback({ data: data });
	            self._isInit=true;
	        }, this._delay);
        }
    }
};

/**
 * 及时加载工程树
 */
function loadProjectTree(){
	var url = "codeGenerateController/redProjectJsonStr.do";
	var params={
		projectDir:$("#projectDir").val()
	};
	ajaxRequest(url,params,function(data) {
		if (data.resCode == "200") {
			var treeData = eval("(" + data.data + ")");
			var treeDataSource = new DataSourceTree({
				data : treeData,
				delay : 100
			});
			$("#projectDirTree").tree({
				cacheItems : true,
				selectable : false,
				dataSource : treeDataSource,
				loadingHTML : '<div class="tree-loading"><i class="fa fa-rotate-right fa-spin"></i></div>'
			});
		} else {
			ajaxError(data.resMsg);
		}
	}, function(XMLHttpRequest, textStatus, errorThrown) {
		ajaxError(errorThrown);
	}, "GET", "JSON");
}

/**
 * 延迟加载目录树
 */
function loadAjaxTree(treeId,isIncludeFile,dir,setDataCallback){
	var url = "codeGenerateController/redDiscJsonStr.do";
	if(!dir){
		dir="";
	}
	var params = {
		dir:dir,
		isIncludeFile:isIncludeFile
	};
	ajaxRequest(url,params,function(data) {
		if (data.resCode == "200") {
			var treeData = eval("(" + data.data + ")");
			console.log(treeData);
			if(setDataCallback&& (typeof setDataCallback === "function")){
				setDataCallback(treeData);
			}else{
				var discTreeAjaxDataSourceTree = new AjaxDataSourceTree({
					data : treeData,
					delay : 100,
					loadData:function(option,callback){
						var nowDir="";
						console.log(option.params);
						if(option.params&&option.params.path){
							nowDir=option.params.path;
						}
						loadAjaxTree(treeId,isIncludeFile,nowDir,callback);
					}
				});
				$("#"+treeId).tree({
					cacheItems : true,
					selectable : false,
					dataSource : discTreeAjaxDataSourceTree,
					loadingHTML : '<div class="tree-loading"><i class="fa fa-rotate-right fa-spin"></i></div>'
				});
			}
		} else {
			ajaxError(data.resMsg);
		}
	}, function(XMLHttpRequest, textStatus, errorThrown) {
		ajaxError(errorThrown);
	}, "GET", "JSON");
}
//即时加载项目结构树
//loadProjectTree();
//延迟加载项目结构树
loadAjaxTree("projectDirTree" , true , $("#projectDir").val());
//延迟加载磁盘目录树
loadAjaxTree("discTree" , false);

/***
 * 测试数据库连接
 */
function testConnection(callBack){
	var jdbcDriver=$("#jdbcDriver").val();
	var jdbcUrl=$("#jdbcUrl").val();
	var jdbcUserName=$("#jdbcUserName").val();
	var jdbcPassword=$("#jdbcPassword").val();
	var url = "codeGenerateController/testConnection.do";
	var params = {
		jdbcDriver:jdbcDriver,
		jdbcUrl:jdbcUrl,
		jdbcUserName:jdbcUserName,
		jdbcPassword:jdbcPassword
	};
	ajaxRequest(url,params,function(data) {
		if (data.resCode == "200") {
			if(typeof callBack === "function"){
				callBack();
			}else{
				alert(data.data);
			}
		} else {
			ajaxError(data.resMsg);
		}
	}, function(XMLHttpRequest, textStatus, errorThrown) {
		ajaxError(errorThrown);
	}, "GET", "JSON");
}
</script>